package com.txsj.scan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


import com.txsj.scan.R;
import com.txsj.scan.bean.AllocationListBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by 冯瑞雨 on 2018/11/5.
 * 库存调拨 历史 aahExpandListView适配器
 */
public class AllocateHistoryAdapeter extends BaseExpandableListAdapter {


    private Context context;
    private List<AllocationListBean> list = new ArrayList<>();

    public AllocateHistoryAdapeter(Context context) {
        this.context = context;
    }

    public List<AllocationListBean> getList() {
        return list;
    }

    public void setList(List<AllocationListBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return list.size() == 0 ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        AHGroupHolder groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_pw_group_explist, null, false);
            groupHolder = new AHGroupHolder(convertView);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (AHGroupHolder) convertView.getTag();
        }
        SimpleDateFormat simpleDateFormat0 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat0.parse(list.get(groupPosition).getAdd_time());
            groupHolder.tvDate.setText(simpleDateFormat1.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        AHChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.ah_item_pw, null);
            childHolder = new AHChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (AHChildHolder) convertView.getTag();
        }
        childHolder.addNumbers.setText(list.get(groupPosition).getList().get(childPosition).getBill_number());
        childHolder.outWarehouse.setText(list.get(groupPosition).getList().get(childPosition).getOut_warehouse_name());
        childHolder.intoWarehouse.setText(list.get(groupPosition).getList().get(childPosition).getInto_warehouse_name());
        childHolder.allocationGoods.setText(list.get(groupPosition).getList().get(childPosition).getAllocation_goods());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                allocateHistoryAdapeterinterface.setOnChildClickListener(groupPosition, childPosition);
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class AHGroupHolder {

        TextView tvDate;
        public AHGroupHolder(View view) {
           tvDate = (TextView) view.findViewById(R.id.tv_date);
        }
    }

    class AHChildHolder {

        TextView addNumbers;
        TextView outWarehouse;
        TextView intoWarehouse;
        TextView allocationGoods;

        public AHChildHolder(View itemView) {
            addNumbers = (TextView) itemView.findViewById(R.id.add_numbers);
            outWarehouse = (TextView) itemView.findViewById(R.id.out_warehouse);
            intoWarehouse = (TextView) itemView.findViewById(R.id.into_warehouse);
            allocationGoods = (TextView) itemView.findViewById(R.id.allocation_goods);
        }
    }

    public void setAllocateHistoryAdapeterinterface(AllocateHistoryAdapeterinterface allocateHistoryAdapeterinterface) {
        this.allocateHistoryAdapeterinterface = allocateHistoryAdapeterinterface;
    }

    public AllocateHistoryAdapeterinterface allocateHistoryAdapeterinterface;

    public interface AllocateHistoryAdapeterinterface {
        void setOnChildClickListener(int groupPosition, int childPosition);
    }
}
